package com.c.yinyuezhushou.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.c.yinyuezhushou.Entity.SongListEntity;
import com.c.yinyuezhushou.Interface.MyInterface;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;
import java.util.List;

public class SongListAdapter1 extends RecyclerView.Adapter{
    private List<SongListEntity> listEntities;
    private MyApplication myApplication;
    private MusicItem musicItem;
    private MyInterface myInterface;


    public SongListAdapter1(List<SongListEntity> list, MyApplication myApplication, MusicItem musicItem, MyInterface myInterface) {
        listEntities = list;
        this.myApplication =myApplication;
        this.musicItem = musicItem;
        this.myInterface = myInterface;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView imageView;
        RelativeLayout relativeLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.islr1_image1);
            textView = itemView.findViewById(R.id.islr1_text);
            relativeLayout = itemView.findViewById(R.id.islr1_re);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_song_list_rec1,parent,false);
        SongListAdapter1.ViewHolder viewHolder = new SongListAdapter1.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        SongListAdapter1.ViewHolder viewHolder = (SongListAdapter1.ViewHolder) holder;
        viewHolder.textView.setText(listEntities.get(position).getListname());

        if (listEntities.get(position).getImageurl().equals("")){
            Glide.with(viewHolder.imageView.getContext()).load(R.drawable.icon_songlist).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder.imageView);
        }else{
            Glide.with(viewHolder.imageView.getContext()).load(listEntities.get(position).getImageurl()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder.imageView);
        }
        viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!myApplication.getMyDButil().isMusicItemTable(musicItem,"SongListMusicItem")){
                    myApplication.getMyDButil().setSongListMusicItem(musicItem,listEntities.get(position).getListname());
                    Toast.makeText(myApplication, "添加成功", Toast.LENGTH_SHORT).show();
                    myInterface.con();
                }else {
                    Toast.makeText(myApplication, "已存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return listEntities.size();
    }
}
